clear all
cd "D:\Taylor_JAERE_2020_DONE"
use TxnDATA_53stores			
			
	duplicates tag store_id hh_id, gen(hhDUP)
	tab hhDUP	

	drop if min == .

	keep if reg_nbr<20 | sc==1

	drop if hhDUP == 0
	drop if hhDUP > 178			

	egen hh_group = group(store_id hh_id)
			
	collapse (mean) expenditure_paid tot_scan paper_bag sc txn_mins (max) DEPT*, ///
			by(store_id store_city hh_id promo_week_id hhDUP hh_group)
		
	gen enacted = 999999

	egen week_group = group(promo_week_id)
	tab week_group
									  
	replace enacted = 53 if ( (store_id ==	997 | store_id ==	1476 | store_id ==	1483 | store_id == 987))														  
	replace enacted = 65 if ( (store_id == 640 | store_id == 786))		
	replace enacted = 79 if (store_id ==	2264)
	replace enacted = 85 if (store_id ==	2327)	
	replace enacted = 93 if ((store_id ==	2306 |  store_id ==	2314 |  store_id ==	2560 | store_id ==	2312 | store_id ==	2317))																						 								
	replace enacted = 106 if ( (store_id == 309 | store_id == 669 | store_id == 676 | ///
							store_id ==	790 | store_id ==	908 | ///
							store_id ==	971 | store_id ==	1224 | ///
							store_id ==	1953 | store_id ==	2089))		
	replace enacted = 107 if store_id == 1583									
	replace enacted = 122 if ( (store_id == 999 | store_id ==	1108 | store_id ==	1138))		
	replace enacted = 128 if ( (store_id == 970))		
	replace enacted = 145 if ( (store_id == 747))			
	replace enacted = 157 if (store_id == 2212)			
	replace enacted = 158 if ((store_id == 788 | store_id ==	2078 | store_id ==	4021))			
	replace enacted = 163 if ((store_id == 1245))

	gen E=(week_group==enacted)	
	tab store_id if E==1	
	gen E_diff = week_group-enacted
	tab E_diff

	gen TREAT = (enacted!=999999)

	gen POST = (E_diff>-1)

	gen visit = 1

	duplicates tag store_id hh_id, gen(hhWkDUP)

	drop if hhWkDUP == 0

	sort hh_group week_group

	bysort hh_group: gen t0 = week_group
	bysort hh_group: gen t1 = week_group[_n+1]
	replace visit = 0 if t1 ==.
	replace t1 = 178 if t1 ==.

	egen city_clust = group(store_city)		
		
	drop if txn_mins == .			
	
	stset t1, id(hh_group) time0(t0) origin(time t0) failure(visit==1) exit(time .)

	reg txn_mins sc expenditure_paid tot_scan DEPT*  
	predict yhat
	gen diff = txn_mins - yhat

	streg diff i.store_id i.week_group, vce(cluster city_clust) dist(gompertz) 	
	estadd local Storefix "Yes", replace
	estadd local weekfix "Yes", replace	
	estadd loc se "Cluster"
	eststo REG1
	
	streg diff i.store_id i.week_group if promo_week_id<201201, vce(cluster city_clust) dist(gompertz) 	
	estadd local Storefix "Yes", replace
	estadd local weekfix "Yes", replace	
	estadd loc se "Cluster"
	eststo REG2	
	
	streg diff POST i.store_id i.week_group, vce(cluster city_clust) dist(gompertz) 			
	estadd local Storefix "Yes", replace
	estadd local weekfix "Yes", replace			
	estadd loc se "Cluster"
	eststo REG3

	label variable diff "Txn. Duration (min.)"				
	label variable POST "Ban Effective (=1)"	

	esttab REG1 REG2 REG3 using "${latex}Tab6_HazardModel.tex", ///
	mtitles("Full Sample" "Pre-Policy" "Full Sample") ///
	align(c) ///
	keep(diff POST) ///
	s(N ll se Storefix weekfix, ///
	label("Num of Obs." "Log Psuedolikihood" "Std Err" "Store FE" "Week-of-Sample FE" )) ///
	replace b(%12.3f) se compress nonotes  ///
	sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label			
	
		
		
		
